package com.trendata.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.trendata.service.LikeService;
import com.trendata.util.JsonResult;

/**
 * 点赞
 * 
 * @author: HuangXiuXiu Email:243559392@qq.com
 */
@Controller
@RequestMapping("/like")
public class LikeController {

	@Autowired
	private LikeService likeService;

	/**
	 * 查询图片的点赞个数,通过分号传入多个图片编号，返回list按照传入的顺序返回数量
	 * 
	 * <p>
	 * url=/like/selectCountByImageIds.do?imageIds=2;3;4;5;6
	 * </p>
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/selectCountByImageIds")
	@ResponseBody
	public String selectCountByImageIds(HttpServletRequest request, HttpServletResponse response) {
		JsonResult result = new JsonResult();
		String imageIds = request.getParameter("imageIds");
		if (imageIds == null || imageIds.equals("")) {
			return result.setMessage("参数错误").toJsonString();
		}
		String[] imageIdArray = imageIds.split(";");
		List<Integer> countList = new ArrayList<Integer>();
		for (String imageId : imageIdArray) {
			int likeNum = likeService.selectCountByImageId(Integer.valueOf(imageId));
			countList.add(likeNum);
		}
		result.setValue(countList);
		result.setSuccess(true);
		return result.toJsonString();
	}

}
